﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using Survey.Common;
using Survey.Common.Validation;
using Survey.Common.Data.Services;
using Survey.Entity.Services;

namespace Survey.Business.Services
{
    public class LanguageFilterValidator : FilterValidatorBase<LanguageFilter, int>
    {
        protected override void Init(RuleCollection<LanguageFilter> rules) { base.Init(rules); }
    }
    public class LanguageValidator : EntityValidatorBase<Language, int, ILanguageDao>
    {
        protected override void Init(RuleCollection<Language> rules)
        {
            // unique key and area
            rules.For(entity => entity.LanguageName).RequireLengthBetween(2, 15)
                .BreakOnError();

            rules.Assure(IsLanguageNameValid);

            rules.IsUnique(entity => Dao.Find(entity.ID, new LanguageFilter { LanguageNameSearch = entity.LanguageName }));
        }

        #region Check

        protected virtual ValidationResult IsLanguageNameValid(Language entity, out ValidationMessage message)
        {
            message = null;
            var languageExists = CultureInfo.GetCultures(CultureTypes.AllCultures)
                .ToList()
                .Any(c => c.Name.Equals(entity.LanguageName, StringComparison.Ordinal));
            if (languageExists)
            {
                return ValidationResult.IsValid;
            }

            message = new ValidationMessage(Str.Messages.LanguageNotExists1)
            {
                entity.LanguageName,
            };
            return ValidationResult.Error;
        }
        #endregion Check
    }
}